package com.cloud.ui;

import com.supermap.desktop.core.ui.controls.GridBagConstraintsHelper;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

public class ServicePanel extends JPanel {
    private final List<JPanel> pageList;
    private static final int DEFAULT_COUNT = 11; // 每页默认十个
    private int pageCount;
    private int currentPage;
    private final JButton buttonNext;
    private final JButton buttonPre;
    private final JLabel pageInfo;
    private final JPanel panelServiceInfo;
    public ServicePanel() {
        this.setLayout(new GridBagLayout());
        pageList = new ArrayList<>();
        buttonNext = new JButton(">");
        pageInfo = new JLabel();
        buttonPre = new JButton("<");
        panelServiceInfo = new JPanel();
        panelServiceInfo.setLayout(new GridBagLayout());
        JPanel panelButtons = new JPanel();
        panelButtons.setLayout(new GridBagLayout());
        panelButtons.add(buttonPre, new GridBagConstraintsHelper(0, 0, 1, 1)
                .setWeight(0, 0)
                .setFill(GridBagConstraints.NONE));
        panelButtons.add(pageInfo, new GridBagConstraintsHelper(1, 0, 1, 1)
                .setWeight(0, 0)
                .setFill(GridBagConstraints.NONE)
                .setInsets(0,5,0,5));
        panelButtons.add(buttonNext, new GridBagConstraintsHelper(2, 0, 1, 1)
                .setWeight(0, 0)
                .setFill(GridBagConstraints.NONE));
        this.add(panelServiceInfo, new GridBagConstraintsHelper(0, 0, 1, 1)
                .setWeight(1, 1)
                .setFill(GridBagConstraints.BOTH));
        this.add(panelButtons, new GridBagConstraintsHelper(0, 1, 1, 1)
                .setWeight(1, 0)
                .setFill(GridBagConstraints.HORIZONTAL));

        registerEvents();
    }

    private void registerEvents() {
        buttonNext.addActionListener(e -> {
            switchPage(currentPage + 1);
        });
        buttonPre.addActionListener(e -> {
            switchPage(currentPage - 1);
        });
    }
    public int getPageCount() {
        return pageCount;
    }

    public void setData(List<PanelServiceInfo> list) {
        pageList.clear();
        panelServiceInfo.removeAll();
        int pageCount = list.size() / DEFAULT_COUNT + (list.size() % DEFAULT_COUNT == 0 ? 0 : 1); // 页数
        this.pageCount = pageCount;
        for (int i = 0; i < pageCount; i++) {
            JPanel jp = new JPanel();
            jp.setLayout(new GridBagLayout());
            jp.add(new JPanel(), new GridBagConstraintsHelper(0, 100, 1, 1)
                    .setFill(GridBagConstraints.HORIZONTAL)
                    .setWeight(1, 1)
                    .setInsets(0, 0, 0, 0));
            pageList.add(jp);
        }

        int currentPage = 0;
        for (int i = 0; i < list.size(); i++) {
            // 每十个当前页面就要+1
            if (i != 0 && i % DEFAULT_COUNT == 0) {
                currentPage ++;
            }
            pageList.get(currentPage).add(list.get(i), new GridBagConstraintsHelper(0, i % DEFAULT_COUNT, 1, 1)
                    .setFill(GridBagConstraints.HORIZONTAL)
                    .setWeight(1, 0)
                    .setInsets(0, 0, 0, 0));
        }
        switchPage(0);
    }

    /**
     * 选择页面
     */
    private void switchPage(int page) {
        if (page >= pageCount || page < 0) {
            return;
        }
        panelServiceInfo.removeAll();
        panelServiceInfo.add(pageList.get(page), new GridBagConstraintsHelper(0,0,1,1)
                .setFill(GridBagConstraints.BOTH)
                .setWeight(1,1)
        );
        panelServiceInfo.repaint();
        currentPage = page;
        int currentPage = this.currentPage + 1; // 代码里面从0开始计数的，设置上去显示统一成+1
        pageInfo.setText(currentPage + "/" + pageCount);
    }
}
